//
//  AddRestaurantTableViewController.swift
//  FoodPin
//
//  Created by 王巍 on 16/5/9.
//  Copyright © 2016年 ww. All rights reserved.
//

import UIKit
import CoreData

class AddRestaurantTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    var restaurant: Restaurant!
    var isVisited = false
    
    @IBOutlet var imageView: UIImageView!
    @IBOutlet var name: UITextField!
    @IBOutlet var type: UITextField!
    @IBOutlet var location: UITextField!
    @IBOutlet var isVisitedLabel: UILabel!
    
    @IBAction func isVisitedTapped(sender: UIButton) {
        if sender.tag == 8001{
            isVisited = true
            isVisitedLabel.text = "我来过了"
        }else{
            isVisited = false
            isVisitedLabel.text = "没来过"
        }
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // 保存
    @IBAction func saveBtnTapped(sender: UIBarButtonItem) {
        let buffer = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
        
        let restaurant = NSEntityDescription.insertNewObjectForEntityForName("Restaurant", inManagedObjectContext: buffer!) as! Restaurant
        
        restaurant.name = name.text!
        restaurant.type = type.text!
        restaurant.location = location.text!
        
        if let image = imageView.image{
            restaurant.image = UIImagePNGRepresentation(image)
        }
        restaurant.isVisited = isVisited
        
        do {
            try buffer?.save()
            
        }catch{
            print(error)
            return
        }
        
        saveRecordToCloud(restaurant)
        
        performSegueWithIdentifier("unwindToHomeList", sender: sender)
        
    }
    
    // 保存数据到 LeanCloud
    func saveRecordToCloud(restaurant: Restaurant!) {
        let record = AVObject(className:  "Restaurant")
        record["name"] = restaurant.name
        record["type"] = restaurant.type
        record["location"] = restaurant.location
        
        let originImg = UIImage(data: restaurant.image!)!
        let scalingFac = originImg.size.width > 1024 ? 1024/originImg.size.width : 1.0
        let scaledImg = UIImage(data: restaurant.image!, scale: scalingFac)!
        
        let imgFile = AVFile(name: "\(restaurant.name).jpg", data: UIImageJPEGRepresentation(scaledImg, 0.8))
        imgFile.saveInBackground()
        
        record["image"] = imgFile
        
        record.saveInBackgroundWithBlock { (isSucceed, e) in
            if let e = e{
                print(e.localizedDescription)
            } else {
                print("保存成功")
            }
        }
    }
  
    // MARK: - Table view data source
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == 0 {
            // 检测是可以访问系统相册
            // .Camera 打开相机 拍照   PhotoLibrary 打开系统相册
            if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary){
                let imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                
                imagePicker.allowsEditing = false
                imagePicker.sourceType = .PhotoLibrary
                
                self.presentViewController(imagePicker, animated: true, completion: nil)
            }
        }
        // 表格取消选中
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        // 平铺
        imageView.contentMode = .ScaleAspectFill
        // 裁边，超出范围的图片
        imageView.clipsToBounds = true
        
        // 使用代码设置布局
        let leftCons = NSLayoutConstraint(item: imageView, attribute: .Leading, relatedBy: .Equal, toItem: imageView.superview, attribute: .Leading, multiplier: 1, constant: 0)
        
        let rightCons = NSLayoutConstraint(item: imageView, attribute: .Trailing, relatedBy: .Equal, toItem: imageView.superview, attribute: .Trailing, multiplier: 1, constant: 0)
        
        let topCons = NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: imageView.superview, attribute: .Top, multiplier: 1, constant: 0)
        
        let bottomCons = NSLayoutConstraint(item: imageView, attribute: .Bottom, relatedBy: .Equal, toItem: imageView.superview, attribute: .Bottom, multiplier: 1, constant: 0)
        
        leftCons.active = true
        rightCons.active = true
        topCons.active = true
        bottomCons.active = true
        
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    
    /*
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }
     */
    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
